package com.songlanyun.modules.rabbitmq.interactmsg.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.songlanyun.common.enums.MarketingConstant;
import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.utils.push.GeTuiUtils;
import com.songlanyun.common.utils.push.Msg;
import com.songlanyun.modules.account.service.AccountService;
import com.songlanyun.modules.marketing.seckill.entity.SeckillActivity;
import com.songlanyun.modules.marketing.seckill.service.SeckillActivityService;
import com.songlanyun.modules.message.entity.PrivateMessage;
import com.songlanyun.modules.message.model.ActivityMessage;
import com.songlanyun.modules.message.model.to.MessageTO;
import com.songlanyun.modules.message.model.to.PrivateMessageTO;
import com.songlanyun.modules.message.service.PrivateMessageService;
import com.songlanyun.modules.rabbitmq.interactmsg.IInteractMsgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 秒杀   --  活动消息
 */
@Service("seckillInteractService")
public class SeckillInteractServiceImpl implements IInteractMsgService {

    @Resource
    private SeckillActivityService seckillActivityService;

    @Resource
    private AccountService accountService;

    @Resource
    private PrivateMessageService privateMessageService;

    @Resource
    private GeTuiUtils geTuiUtils;

    @Override
    public void savePrivateMsg(MessageTO to) {

        SeckillActivity seckillActivity = seckillActivityService.getById(to.getModelId());
        //发送站内消息    给商家下的所有会员
//        List<Long> userIds = accountService.getMemberBySubShop(seckillActivity.getShopId());
        //查询所有的会员
        List<Long> userIds = accountService.getAllMemberId();

        List<PrivateMessage> list = new ArrayList<>();
        String remark = String.format("秒杀活动【%s】即将开始，快来参加吧！", seckillActivity.getName());
        ActivityMessage activityMessage = new ActivityMessage(MarketingConstant.ActivityType.SECKILL, seckillActivity.getId(), seckillActivity.getName(), seckillActivity.getImgUrl());

        userIds.forEach(u -> {
            PrivateMessage privateMessage = new PrivateMessage(
                    new PrivateMessageTO(
                            u,
                            PrivateMessageConstant.MessageType.ACTIVITY_MESSAGE,
                            activityMessage,
                            remark)
            );
            list.add(privateMessage);
        });

        privateMessageService.saveBatch(list);

        //批量推
        if (CollectionUtil.isNotEmpty(list)) {
            geTuiUtils.pushByUserIds(
                    Msg.builder()
                            .uidList(userIds)
                            .title(PrivateMessageConstant.MessageType.ACTIVITY_MESSAGE.getMsg())
                            .content(remark)
                            .msg(list.get(0).pushMessageBody())
                            .build()
            );
        }

    }
}
